package zengjin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.PageResponse;
import zengjin.mapper.ImageMapper;
import zengjin.mapper.JobseekerMapper;
import zengjin.model.dto.jobseeker.PageJobseekerRequest;
import zengjin.model.dto.jobseeker.UpdateJobseekerPersonalDto;
import zengjin.model.en.Employer;
import zengjin.model.en.Image;
import zengjin.model.en.Jobseeker;
import zengjin.model.vo.jobseeker.JobseekerVO;
import zengjin.service.JobseekerService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【jobseeker】的数据库操作Service实现
 * @createDate 2025-01-02 14:22:19
 */
@Service
public class JobseekerServiceImpl extends ServiceImpl<JobseekerMapper, Jobseeker>
        implements JobseekerService {

    @Resource
    private JobseekerMapper jobseekerMapper;

    @Resource
    private ImageMapper imageMapper;

    @Override
    public boolean updateJobseekerPersonal(UpdateJobseekerPersonalDto updateJobseekerPersonalDto) {
        //1.获取参数
        Long jobseekerId = updateJobseekerPersonalDto.getJobseekerId();
        String jobseekerUsername = updateJobseekerPersonalDto.getJobseekerUsername();
        String jobseekerPhone = updateJobseekerPersonalDto.getJobseekerPhone();
        String jobseekerName = updateJobseekerPersonalDto.getJobseekerName();
        String jobseekerGender = updateJobseekerPersonalDto.getJobseekerGender();
        Date jobseekerBirthdate = updateJobseekerPersonalDto.getJobseekerBirthdate();
        String jobseekerEducationLevel = updateJobseekerPersonalDto.getJobseekerEducationLevel();
        String jobseekerMajor = updateJobseekerPersonalDto.getJobseekerMajor();
        String userAvatarUrl = updateJobseekerPersonalDto.getUserAvatarUrl();

        //2.更新头像信息
        int update = 0;
        if (StrUtil.isNotBlank(userAvatarUrl)) {
            LambdaUpdateWrapper<Image> updateImgWrapper = new LambdaUpdateWrapper<>();
            updateImgWrapper.eq(Image::getImageNameId, jobseekerId)
                    .eq(Image::getImageName, "jobseeker_avatar")
                    .set(StrUtil.isNotBlank(userAvatarUrl), Image::getImageUrl, userAvatarUrl);
            update = imageMapper.update(null, updateImgWrapper);
        }

        //3.更新求职者信息
        LambdaUpdateWrapper<Jobseeker> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Jobseeker::getJobseekerId, jobseekerId)
                .set(Jobseeker::getJobseekerUsername, jobseekerUsername)
                .set(Jobseeker::getJobseekerPhone, jobseekerPhone)
                .set(Jobseeker::getJobseekerName, jobseekerName)
                .set(Jobseeker::getJobseekerGender, jobseekerGender)
                .set(Jobseeker::getJobseekerBirthdate, jobseekerBirthdate)
                .set(Jobseeker::getJobseekerEducationLevel, jobseekerEducationLevel)
                .set(Jobseeker::getJobseekerMajor, jobseekerMajor);
        return (jobseekerMapper.update(null, updateWrapper) + update) > 0;
    }

    @Override
    public PageResponse<JobseekerVO> getJobseekerAdminPage(PageJobseekerRequest pageJobseekerRequest) {
        //1.获取参数
        String searchName = pageJobseekerRequest.getSearchName();
        String jobseekerIsVerified = pageJobseekerRequest.getJobseekerIsVerified();
        String jobseekerStatus = pageJobseekerRequest.getJobseekerStatus();
        String jobseekerEducationLevel = pageJobseekerRequest.getJobseekerEducationLevel();

        //2.构建分页条件
        Page<Jobseeker> page = pageJobseekerRequest.toPageDefaultSortByUpdateTime();

        //3.查询出集合
        QueryWrapper<Jobseeker> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(jobseekerIsVerified), "jobseeker_is_verified", jobseekerIsVerified)
                .eq(StrUtil.isNotBlank(jobseekerStatus), "jobseeker_status", jobseekerStatus)
                .eq(StrUtil.isNotBlank(jobseekerEducationLevel), "jobseeker_education_level", jobseekerEducationLevel)
                .and(StrUtil.isNotBlank(searchName),
                        wrapper -> wrapper.like("jobseeker_major", searchName)
                                .or()
                                .like("jobseeker_username", searchName))
                .orderByDesc("update_time");
        List<Jobseeker> jobseekers = jobseekerMapper.selectList(queryWrapper);
        List<JobseekerVO> jobseekerVOS = new ArrayList<>();
        jobseekers.forEach(jobseeker -> {
            JobseekerVO jobseekerVO = BeanUtil.copyProperties(jobseeker, JobseekerVO.class);
            //查询出头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "jobseeker_avatar");
            imageQueryWrapper.eq("image_name_id", jobseeker.getJobseekerId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                jobseekerVO.setUserAvatarUrl(image.getImageUrl());
            }
            jobseekerVOS.add(jobseekerVO);
        });
        return PageResponse.of(page, jobseekerVOS);

    }

    @Override
    public boolean updateJobseekerVerified(Long jobseekerId, String jobseekerIsVerified) {
        //1.构造条件
        LambdaUpdateWrapper<Jobseeker> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Jobseeker::getJobseekerId, jobseekerId)
                .set(Jobseeker::getJobseekerIsVerified, jobseekerIsVerified);
        //2.更新
        return jobseekerMapper.update(null, updateWrapper) > 0;
    }
}




